﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL;
using System.Text;
using HKEA.CMS.Core.BLL.Tbl;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// BranchOffice 的摘要说明
    /// </summary>
    public class BranchOffice : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strRet = string.Empty;
            //查询
            if (context.Request.QueryString["optiontype"] == "get")
            {
                strRet = GetVideoList(context);
            }
            //假删除
            else if (context.Request.QueryString["optiontype"] == "del")
            {
                strRet = DeleteVideoRelated(context);
            }

            context.Response.Write(strRet.ToString());

        }
        //获取列表
        public string GetVideoList(HttpContext context)
        {
            int customeruid = 0;
            int.TryParse(context.Request.QueryString["cuid"], out customeruid);
            StringBuilder strBuilder = new StringBuilder();
            if (customeruid > 0)
            {
                tblMediaRoom mediaroomENT = new tblMediaRoom();
                tblMediaRoomBLL mediaroomBLL = new tblMediaRoomBLL();
                mediaroomENT.CustomerUID = customeruid;
                mediaroomENT.Category = Constant.MediaRoomCategory.branch_video;
                var list = mediaroomBLL.GetListByENT(mediaroomENT);
                if (list.Count > 0)
                {
                    strBuilder.Append("");
                    int i = 1;
                    string strFormAt = "{{\"roomUID\":\"{0}\",\"FileName\":\"{1}\"}}";
                    foreach (var temp in list)
                    {
                        strBuilder.Append(string.Format(strFormAt, temp.MediaRoomUID, temp.FileName));
                        if (i < list.Count)
                        {
                            strBuilder.Append(",");
                        }
                        i++;
                    }
                }
                else { strBuilder.Append(ResourceManage.GetResourceString("msg200000")); }
            }
            else { strBuilder.Append(ResourceManage.GetResourceString("msg200001")); }
            return strBuilder.ToString();
        }
        
        public string DeleteVideoRelated(HttpContext context)
        {
            string strRet = string.Empty;
            #region
            //TODO 需完善数据校验、权限验证等操作
            //int bouid = 0;
            //int customeruid = 0;
            //int relateduid = 0;
            //if (int.TryParse(context.Request.QueryString["bouid"], out bouid) && int.TryParse(context.Request.QueryString["cuid"], out customeruid) && int.TryParse(context.Request.QueryString["reluid"], out relateduid))
            //{
            //    tblMediaRoom_Related relatedENT = new tblMediaRoom_Related();
            //    tblMediaRoomRelatedBLL relatedBLL = new tblMediaRoomRelatedBLL();
            //    relatedENT.MediaRoomRelatedUID = relateduid;
            //    relatedENT = relatedBLL.GetEntity(relatedENT);

            //    tblMediaRoom mediaENT = new tblMediaRoom();
            //    tblMediaRoomBLL mediaBLL = new tblMediaRoomBLL();
            //    mediaENT.MediaRoomUID = (int)relatedENT.MediaRoomUID;
            //    mediaENT.CustomerUID = customeruid;
            //}
            #endregion
            int relateduid = 0;
            if (int.TryParse(context.Request.QueryString["reluid"], out relateduid))
            {
                tblMediaRoom_Related relatedENT = new tblMediaRoom_Related();
                tblMediaRoomRelatedBLL relatedBLL = new tblMediaRoomRelatedBLL();
                relatedENT.MediaRoomRelatedUID = relateduid;
                relatedENT.Deleted = 0;
                relatedENT = relatedBLL.GetEntity(relatedENT);
                if (relatedENT != null)
                {
                    relatedENT.Deleted = 1;
                    int i = relatedBLL.UpdateENT(relatedENT);
                    if (i > 0)
                    { strRet = ResourceManage.GetResourceString("msg200011"); }
                    else
                    { strRet = ResourceManage.GetResourceString("msg200010"); }
                }
                else
                { strRet = ResourceManage.GetResourceString("msg200014"); }
            }
            else
            { strRet = ResourceManage.GetResourceString("msg200001"); }
            return null;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}